/*
*
* Copyright (C) 2010, dbzhang800
* All rights reserved.
*/

#include <windows.h>
#include <string.h>
#include <stdio.h>

int main()
{
    HANDLE hFileMap = CreateFileMappingW(INVALID_HANDLE_VALUE,
            NULL,
            PAGE_READWRITE,
            0,
            4 * 1024,
            L"dbzhang800-mmfshare");
    if (hFileMap != NULL) {
        if (GetLastError() == ERROR_ALREADY_EXISTS) {
            printf("Mapping already exists.\n");
            PVOID pView = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
            printf("\n%s\n\n", (char*)pView);
        } else {
            printf("Mapping created successfully.\n");
            PVOID pView = MapViewOfFile(hFileMap, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
            if (pView != NULL) {
                strncpy((char*)pView, "Hello From Memory-Mapped File Sharing.", 200);
                UnmapViewOfFile(pView);
            }
        }
        system("pause");
        CloseHandle(hFileMap);
    }

    return 0;
}
